/**
 * BottomModuleView.java包描述<br>
 * 描述区
 *
 * @author haodai<br>
 * date: 18/5/25
 */
package com.zzh.views.bottom_module;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.support.annotation.IdRes;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzh.views.R;

/**
 * Created by haodai on 18/5/25.
 */
public class BottomModuleView extends FrameLayout implements View.OnClickListener {

    private Context mContext;
    private ColorStateList colorStateList;

    private ImageView mTabOneHint;
    private ImageView mTabTwoHint;
    private ImageView mTabThreeHint;
    private ImageView mTabFourHint;

    private ImageView mTabOne;
    private ImageView mTabTwo;
    private ImageView mTabThree;
    private ImageView mTabFour;

    private TextView mTabOneText;
    private TextView mTabTwoText;
    private TextView mTabThreeText;
    private TextView mTabFourText;


    private OnModuleItemListener onModuleItemListener;


    public interface OnModuleItemListener {
        void onItems(int position);
    }

    public void setOnModuleItemListener(OnModuleItemListener onModuleItemListener) {
        this.onModuleItemListener = onModuleItemListener;
    }

    public BottomModuleView(Context context) {
        this(context, null);
    }

    public BottomModuleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomModuleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
        View view = initView(context);
        setListener();
        addView(view);
    }

    private void init(Context context, AttributeSet attrs) {
        this.mContext = context;
    }


    private View initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.bottom_module_view, null);
        mTabOne = fintView(view, R.id.tab_one);
        mTabTwo = fintView(view, R.id.tab_two);
        mTabThree = fintView(view, R.id.tab_three);
        mTabFour = fintView(view, R.id.tab_four);

        mTabOneHint = fintView(view, R.id.tab_one_hint);
        mTabTwoHint = fintView(view, R.id.tab_two_hint);
        mTabThreeHint = fintView(view, R.id.tab_three_hint);
        mTabFourHint = fintView(view, R.id.tab_four_hint);

        mTabOneText = fintView(view, R.id.tab_one_text);
        mTabTwoText = fintView(view, R.id.tab_two_text);
        mTabThreeText = fintView(view, R.id.tab_three_text);
        mTabFourText = fintView(view, R.id.tab_four_text);
        return view;
    }

    private void setListener() {
        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
        mTabThree.setOnClickListener(this);
        mTabFour.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (onModuleItemListener == null) return;
        int id = v.getId();
        selectStatus(id);
        if (id == R.id.tab_one) {
            onModuleItemListener.onItems(1);
        } else if (id == R.id.tab_two) {
            onModuleItemListener.onItems(2);
        } else if (id == R.id.tab_three) {
            onModuleItemListener.onItems(3);
        } else if (id == R.id.tab_four) {
            onModuleItemListener.onItems(4);
        } else {
            onModuleItemListener.onItems(0);
        }
    }

    private void selectStatus(@IdRes int rId) {
        Resources resources = mContext.getResources();

    }

    /**
     * 对TextView设置不同状态时其文字颜色。
     */
    private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
        int[] colors = new int[]{pressed, focused, normal, focused, unable, normal};
        int[][] states = new int[6][];
        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_enabled};
        states[3] = new int[]{android.R.attr.state_focused};
        states[4] = new int[]{android.R.attr.state_window_focused};
        states[5] = new int[]{};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }


    private <T extends View> T fintView(View view, @IdRes int rId) {
        return (T) view.findViewById(rId);
    }


}
